<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<%--<c:set var="upmsAgents" value="${upmsAgents}"/>--%>

<div id="createDialog" class="container-fluid">
    <div class="row">
        <form id="createForm" method="post" class="form-horizontal" action="">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="name">
                    <span class="help-inline"><font color="red">*</font> </span>
                    名称：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="name" name="name" placeholder="名称" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="price">
                    <span class="help-inline"><font color="red">*</font> </span>
                    预付金金额(元)：</label>
                <div class="col-sm-5">
                    <input type="number" step="0.01" class="form-control" id="price" name="price" placeholder="预付金金额" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="beginTime">
                    <span class="help-inline"><font color="red">*</font> </span>
                    开始时间：</label>
                <div class="col-sm-5">
                        <input type="text" id="beginTime"  class="form-control" data-date-format="yyyy-mm-dd" placeholder="开始时间">
                        <input type="text" hidden name="beginTime" >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="endTime">
                    <span class="help-inline"><font color="red">*</font> </span>
                    结束时间：</label>
                <div class="col-sm-5">
                    <input type="text" id="endTime" class="form-control" data-date-format="yyyy-mm-dd" placeholder="结束时间">
                        <input type="text" hidden name="endTime" >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="status" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    状态：</label>
                <div class="col-sm-5">
                    <select id="status" name="status" class="form-control">
                        <option value="1" selected="selected">启用</option>
                        <option value="0">禁止</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="description">描述：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="description" name="description" placeholder="描述"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="createDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function validateForm(){
        return $( "#createForm" ).validate( {
            rules: {
                price: "required",
                beginTime: "required",
                endTime: "required",
                status: "required",
                name: {
                    required: true,
                    maxlength: 50
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                name: "名称是必填项。",
                pid: "上级代理是必填项。",
                name: {
                    required: "名称是必填项。",
                    maxlength: "名称长度超出64个字符。"
                },
                description: {
                    maxlength: "描述长度超出500个字符"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

    $(function(){
        $('#beginTime,#endTime').datepicker({
//            format: 'yyyy-mm-dd'
        });
    });

    <%--$(function(){--%>
        <%--//初始化树形select控件--%>
        <%--$("#agent").bootstrapCombotree({--%>
            <%--data: ${upmsAgents},//data应符合实例的data格式--%>
            <%--defaultLable:"请选择",--%>
            <%--name : 'pid'//combotree值域的name，可以用在表单提交--%>
        <%--})--%>
    <%--});--%>

    function createSubmit() {
        if(validateForm().form()){
            if($("#beginTime").val())
                $("input[name='beginTime']").val(getLongFromatDateStr($("#beginTime").val()));
            if($("#endTime").val())
                $("input[name='endTime']").val(getLongFromatDateStr($("#endTime").val()));
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/deposit/create',
                data: $('#createForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        createDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>